#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_icon=wincontrolhotkeys.ico
#AutoIt3Wrapper_outfile=wincontrolhotkeys.exe
#AutoIt3Wrapper_Res_Description=Control window and launch programs using hotkeys
#AutoIt3Wrapper_Res_Fileversion=0.4.1.6
#AutoIt3Wrapper_Res_Fileversion_AutoIncrement=p
#AutoIt3Wrapper_Res_LegalCopyright=GNU GPL v3
#AutoIt3Wrapper_Res_Language=1033
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#Include <GuiToolBar.au3>
#include <EditConstants.au3>
#include <Misc.au3>
Opt("TrayMenuMode",3)
Global $ontopkeys, $hidekeys, $transkeys, $keyscurrent, $activewintitle
Global $inifile = StringTrimRight(@ScriptFullPath,3) & "ini"
Global $builtinfunctions = "AlwaysOnTop|Min2Tray|WindowTransparency|CopyAsText|CopyAsTextFilename||TaskbarHideButton|TaskbarShowButton||MoveLeftEdge|MoveRightEdge|MoveBottomEdge|MoveTopEdge||MoveLeftHalf|MoveRightHalf|MoveTopHalf|MoveBottomHalf||MoveLeft|MoveRight|MoveUp|MoveDown"
Global $movestep = 10
FileMove("default.ini",$inifile)
If ProcessExists(IniRead($inifile,"misc","pid","")) Then Exit
IniWrite($inifile,"misc","pid",@AutoItPID)
OnAutoItExitRegister("fnExit")
$hiddenwinmenu = TrayCreateMenu("Hidden windows(0)")
$showallwin = TrayCreateItem("Show all hidden windows")
TrayCreateItem("")
$keysetting = TrayCreateItem("Hotkey settings")
$currentkeys = TrayCreateItem("Current Hotkeys")
TrayCreateItem("")
$iabout = TrayCreateItem("About")
TrayCreateItem("")
$iexist = TrayCreateItem("Exit")
$hiddenwin = IniReadSection($inifile,"hiddenwin")
If @error = 0 Then
   For $i = 1 To $hiddenwin[0][0]
   	$trayid = TrayCreateItem($hiddenwin[$i][0],$hiddenwinmenu)
   	IniWrite($inifile,"hiddenwin",$hiddenwin[$i][0],$trayid)
   Next
   TrayItemSetText($hiddenwinmenu,"Hidden Windows(" & $hiddenwin[0][0] & ")")
EndIf
global $progs = IniReadSection($inifile,"keys")
If @error = 0 Then
   For $i = 1 to $progs[0][0]
   	$prog = $progs[$i][0]
   	$keys = $progs[$i][1]
   	If StringInStr($builtinfunctions,$prog) And $keys <> "" Then
   		HotKeySet($keys,$prog)
   	Else
   		HotKeySet($keys,"fnLaunchProg")
   	EndIf
    Next
EndIf
While 1
   $msg1 = TrayGetMsg()
   Select
   	Case $msg1 = 0
   		ContinueLoop
   	Case $msg1 = $iexist
   		Exit
   	Case $msg1 = $keysetting
   		fnKeySettings()
   	case $msg1 = $currentkeys
   		fnShowKeys()
   	Case $msg1 = $iabout
   		fnAbout()
   	Case $msg1 = $showallwin
   		fnShowAll()
   	Case $msg1 > 0 And $msg1 <> $iexist
   		fnShowWin($msg1)
   EndSelect
WEnd

Func fnAbout()
   $version = "wincontrolhotkeys v" & FileGetVersion(@ScriptFullPath)
   $copy = StringFormat("COPYING\r\n  Copyright (C) 2011  Tri Le <trile7 at gmail dot com>\r\n  This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.\r\n  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.\r\n  You should have received a copy of the GNU General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.")
   $feature = StringFormat("FEATURE\r\n  This program registers hotkeys in Windows to keep window on top, minimize window to system tray, change window transparency, hide window from taskbar, copy item as text to clipboard, and launch program")
   $changlog = StringFormat("CHANGELOG\r\n  ")
   $changlog = $changlog & StringFormat("v0.1\r\n  - initial release\r\n  ")
   $changlog = $changlog & StringFormat("v0.3\r\n  - add about dialog\r\n  - minor fixes\r\n  ")
   $changlog = $changlog & StringFormat("v0.4\r\n  - add copy filename as text\r\n  ")
   $changlog = $changlog & StringFormat("v0.4.1\r\n  - fix sticky modifier keys\r\n  ")
   $about = $version & @CRLF & @CRLF & $feature & @CRLF & @CRLF & $changlog & @CRLF & @CRLF & $copy
   #Region ### START Koda GUI section ### Form=
   $fAbout = GUICreate("wincontrolhotkeys", 428, 244, 192, 114)
   $Edit1 = GUICtrlCreateEdit($about, 0, 0, 425, 209, $ES_AUTOVSCROLL + $WS_VSCROLL + $ES_READONLY)
   $btnOK = GUICtrlCreateButton("OK", 312, 216, 75, 25)
   $btnHome = GUICtrlCreateButton("Home", 40, 216, 75, 25)
   GUISetState(@SW_SHOW)
   #EndRegion ### END Koda GUI section ###
   While 1
   	$nMsg = GUIGetMsg()
   	Switch $nMsg
   		Case $GUI_EVENT_CLOSE, $btnOK
   			GUIDelete($fAbout)
   			ExitLoop
   		Case $btnHome
   			ShellExecute('http://code.google.com/p/wincontrolhotkeys')
   	EndSwitch
   WEnd
EndFunc

Func fnUnstuck()
  Local $iT = TimerInit()
  While _IsPressed("10") Or _IsPressed("11") Or _IsPressed("12") Or _IsPressed("5B") Or _IsPressed("5C")
    If TimerDiff($iT) > 1000 Then
      If _IsPressed("10") Then
        Send("{SHIFTDOWN}")
        Send("{SHIFTUP}")
      ElseIf _IsPressed("11") Then
        Send("{CTRLDOWN}")
        Send("{CTRLUP}")
      ElseIf _IsPressed("12") Then
        Send("{ALTDOWN}")
        Send("{ALTUP}")
      ElseIf _IsPressed("5B") Then
        Send("{LWINDOWN}")
        Send("{LWINUP}")
      ElseIf _IsPressed("5C") Then
        Send("{RWINDOWN}")
        Send("{RWINUP}")
      EndIf
    EndIf
    If TimerDiff($iT) > 2000 Then
      If _IsPressed("10") Then
        MsgBox(48,"winctrolhotkeys","Shift key is stuck.  Please press it again")
      ElseIf _IsPressed("11") Then
        MsgBox(48,"winctrolhotkeys","Ctrl key is stuck.  Please press it again")
      ElseIf _IsPressed("12") Then
        MsgBox(48,"winctrolhotkeys","Alt key is stuck.  Please press it again")
      ElseIf _IsPressed("5B") Then
        MsgBox(48,"winctrolhotkeys","Win-L key is stuck.  Please press it again")
      ElseIf _IsPressed("5C") Then
        MsgBox(48,"winctrolhotkeys","Win-R key is stuck.  Please press it again")
      EndIf
    EndIf
    Sleep(50)
  WEnd
EndFunc

Func fnShowKeys()
   $str = ""
   For $i = 1 To $progs[0][0]
   	$j = $progs[$i][1]
   	If $j = "" Then ContinueLoop
   	$j = StringReplace($j,"#","{Win}")
   	$j = StringReplace($j,"^","{Ctrl}")
   	$j = StringReplace($j,"!","{Alt}")
   	$j = StringReplace($j,"+","{Shift}")
   	$str = $progs[$i][0] & " = " & $j & @LF & $str
   Next
   MsgBox(0,"Current Hotkeys",$str)
EndFunc

Func fnExit()
   If @exitCode <> 2 Then
   	fnShowAll()
   	IniWrite($inifile,"misc","pid","")
    EndIf
    fnUnstuck()
EndFunc

Func CopyAsText()
   Send("^c")
   Sleep(200)
   ClipPut(ClipGet())
  fnUnstuck()
EndFunc

Func CopyAsTextFilename()
   Send("^c")
   Sleep(200)
   $i=StringSplit(ClipGet(),"\")
   ClipPut($i[$i[0]])
  fnUnstuck()
EndFunc

Func fnLaunchProg()
   Local $j
   For $i = 1 To $progs[0][0]
   	$prog = $progs[$i][0]
   	$keys = StringReplace($progs[$i][1],"{usesel}","")
   	$usesel = @extended
   	If $progs[$i][1] = @HotKeyPressed Then
   		If $usesel = 1 Then
   			ClipPut("")
   			Sleep(200)
   			Send("^c")
   			Sleep(200)
   			$f = ClipGet()
   			If FileExists($f) Then $j = MsgBox(4,$prog,"Open " & $f & "?")
   			If $j = 6 Then
   				$workingdir=StringMid($prog, 1, StringInStr($prog, "\", 0, -1)-1)
          Run($prog & " '" & $f & "'", $workingdir)
   				ExitLoop
   			EndIf
   		EndIf
      $workingdir=StringMid($prog, 1, StringInStr($prog, "\", 0, -1)-1)
   		Run($prog, $workingdir)
   		ExitLoop
   	EndIf
   Next
  fnUnstuck()
EndFunc

Func fnSetKeys($chkC,$chkW,$chkA,$chkS,$ikey,$prog,$val,$chkUseSel)
   GUICtrlSetState($chkC,$GUI_UNCHECKED)
   GUICtrlSetState($chkW,$GUI_UNCHECKED)
   GUICtrlSetState($chkA,$GUI_UNCHECKED)
   GUICtrlSetState($chkS,$GUI_UNCHECKED)
   GUICtrlSetState($chkUseSel,$GUI_UNCHECKED)
   $keyscurrent = IniRead($inifile,"keys",$prog,$val)
   If $keyscurrent = "" Then Return
   If StringInStr($keyscurrent,"^") Then GUICtrlSetState($chkC,$GUI_CHECKED)
   If StringInStr($keyscurrent,"#") Then GUICtrlSetState($chkW,$GUI_CHECKED)
   If StringInStr($keyscurrent,"!") Then GUICtrlSetState($chkA,$GUI_CHECKED)
   If StringInStr($keyscurrent,"+") Then GUICtrlSetState($chkS,$GUI_CHECKED)
   $key = StringReplace($keyscurrent,"{usesel}","")
   If @extended = 1 Then GUICtrlSetState($chkUseSel,$GUI_CHECKED)
   $k = StringRegExp($key,"{(.*)}",1)
   GUICtrlSetData($ikey,$k[0])
EndFunc

Func fnKeySettings()
   #Region ### START Koda GUI section ### Form=
   $fHotkeys = GUICreate("Hotkeys", 263, 217, 234, 119)
   GUICtrlCreateGroup("Hotkeys", 192, 0, 65, 153)
   $chkC = GUICtrlCreateCheckbox("Ctrl", 200, 24, 40, 17)
   $chkW = GUICtrlCreateCheckbox("Win", 200, 48, 40, 17)
   $chkA = GUICtrlCreateCheckbox("Alt", 200, 72, 40, 17)
   $chkS = GUICtrlCreateCheckbox("Shift", 200, 96, 41, 17)
   $ikey = GUICtrlCreateInput("", 200, 120, 50, 21)
   GUICtrlSetLimit(-1, 12)
   GUICtrlCreateGroup("", -99, -99, 1, 1)
   GUICtrlCreateGroup("", 0, 0, 185, 153)
   $rBuiltin = GUICtrlCreateRadio("Builtin functions", 8, 16, 97, 17)
   GUICtrlSetState(-1,$GUI_CHECKED)
   $cbFunction = GUICtrlCreateCombo("", 5, 36, 175, 25)
   GUICtrlSetData(-1,$builtinfunctions)
   $rProg = GUICtrlCreateRadio("Program", 8, 72, 65, 17)
   $cbProgpath = GUICtrlCreateCombo("", 5, 92, 155, 25)
   $progs = IniReadSection($inifile,"keys")
   If @error = 0 Then
   	$list = ""
   	For $i = 1 To $progs[0][0]
   		$prog = $progs[$i][0]
   		If StringInStr($builtinfunctions,$prog) = 0 Then $list = $list & "|" & $prog
   	Next
   	GUICtrlSetData(-1, $list)
   EndIf
   GUICtrlSetState(-1, $GUI_DISABLE)
   $btnFileSelDia = GUICtrlCreateButton(".....", 162, 92, 19, 21)
   $chkUseSel = GUICtrlCreateCheckbox("Use current selection", 8, 120, 169, 17)
   GUICtrlSetTip(-1, "If checked, it will attempt pass current selection or clipboard content to program")
   GUICtrlSetState(-1, $GUI_DISABLE)
   GUICtrlCreateGroup("", -99, -99, 1, 1)
   $btnAdd = GUICtrlCreateButton("Add/Change", 164, 160, 75, 25)
   $btnRem = GUICtrlCreateButton("Remove", 20, 160, 75, 25)
   $chkStartup = GUICtrlCreateCheckbox("run on window startup", 5, 192, 137, 17)
   $startuplnk = FileGetShortcut(@StartupDir & "\" & StringTrimRight(@ScriptName,3) & "lnk")
   If @error = 1 Then $startuplnk = FileGetShortcut(@StartupCommonDir & "\" & StringTrimRight(@ScriptName,3) & "lnk")
   If @error = 0 Then
   	If $startuplnk[0] = @ScriptFullPath Then GUICtrlSetState(-1,$GUI_CHECKED)
   EndIf
   GUISetState(@SW_SHOW)
   #EndRegion ### END Koda GUI section ###

   While 1
   	$gmsg = GUIGetMsg()
   	Switch $gmsg
   		Case 0
   			ContinueLoop
   		Case $GUI_EVENT_CLOSE
   			GUIDelete($fHotkeys)
   			ExitLoop
   		Case $rBuiltin
   			GUICtrlSetState($cbProgpath,$GUI_DISABLE)
   			GUICtrlSetState($chkUseSel,$GUI_DISABLE)
   			GUICtrlSetState($cbFunction,$GUI_ENABLE)
   			fnSetKeys($chkC,$chkW,$chkA,$chkS,$ikey,GUICtrlRead($cbFunction),"",$chkUseSel)
   		Case $cbFunction
   			fnSetKeys($chkC,$chkW,$chkA,$chkS,$ikey,GUICtrlRead($cbFunction),"",$chkUseSel)
   		Case $rProg
   			GUICtrlSetState($cbProgpath,$GUI_ENABLE)
   			GUICtrlSetState($chkUseSel,$GUI_ENABLE)
   			GUICtrlSetState($cbFunction,$GUI_DISABLE)
   			fnSetKeys($chkC,$chkW,$chkA,$chkS,$ikey,GUICtrlRead($cbProgpath),"",$chkUseSel)
   		Case $cbProgpath
   			fnSetKeys($chkC,$chkW,$chkA,$chkS,$ikey,GUICtrlRead($cbProgpath),"",$chkUseSel)
   		Case $chkStartup
   			If GUICtrlRead($chkStartup) = $GUI_CHECKED Then
   				FileCreateShortcut(@ScriptFullPath,@StartupDir & "\" & StringTrimRight(@ScriptName,3) & "lnk")
   			Else
   				FileDelete(@StartupDir & "\" & StringTrimRight(@ScriptName,3) & "lnk")
   				FileDelete(@StartupCommonDir & "\" & StringTrimRight(@ScriptName,3) & "lnk")
   			EndIf
   		Case $btnFileSelDia
   			If GUICtrlRead($rProg) = $GUI_CHECKED Then
   				$dir = IniRead($inifile,"misc","recentfile",@ProgramFilesDir)
   				$i = FileOpenDialog("Select program",$dir,"Executables (*.exe)|Batch (*.bat)|All (*.*)",1)
   				If @error = 0 Then
   					GUICtrlSetData($cbProgpath,$i,$i)
   					IniWrite($inifile,"misc","recentfile",$i)
   				EndIf
   			EndIf
   		Case $btnAdd
   			$keys = ""
   			$err = ""
   			$list = ""
   			If GUICtrlRead($chkC) = $GUI_CHECKED Then $keys = "^"
   			If GUICtrlRead($chkW) = $GUI_CHECKED Then $keys = $keys & "#"
   			If GUICtrlRead($chkA) = $GUI_CHECKED Then $keys = $keys & "!"
   			If GUICtrlRead($chkS) = $GUI_CHECKED Then $keys = $keys & "+"
   			$key = GUICtrlRead($ikey)
   			If $keys = "" Or $key = "" Then
   				MsgBox(16,"Hotkeys Error","Hotkeys must have at least one of the modifiers (Ctrl, Win, Alt, Shift) and a letter/number!")
   			Else
   				$keys = $keys & "{" & $key & "}"
   				If GUICtrlRead($chkUseSel) = $GUI_CHECKED Then $keys = $keys & "{usesel}"
   				$progs = IniReadSection($inifile,"keys")
   				If @error = 0 Then
   					For $i = 1 To $progs[0][0]
   						$prog = $progs[$i][0]
   						If StringInStr($builtinfunctions,$prog) = 0 Then $list = $list & "|" & $prog
   						If $progs[$i][1] = $keys Then
   							$err = "The selected keys are already taken by " & $prog
   							ExitLoop
   						EndIf
   					Next
   				EndIf
   				If $err = "" Then
   					If GUICtrlRead($rBuiltin) = $GUI_CHECKED Then
   						$prog = GUICtrlRead($cbFunction)
   						If $prog = "" Then $err = "Choose a function from dropdown menu"
   					ElseIf GUICtrlRead($rProg) = $GUI_CHECKED Then
   						$prog = GUICtrlRead($cbProgpath)
   						If FileExists($prog) = 0 Then $err = "Program does not exist.  Fullpath is required (i.e. c:\windows\explorer.exe"
   					EndIf
   					If $err = "" Then
   						If $keyscurrent <> "" Then HotKeySet($keyscurrent)
   						If IniWrite($inifile,"keys",$prog,$keys) Then
   							If GUICtrlRead($rProg) = $GUI_CHECKED Then
   								HotKeySet($keys,"fnLaunchProg")
   							Else
   								HotKeySet($keys,$prog)
   							EndIf
   							If StringInStr($list,$prog) = 0 Then $list = $list & "|" & $prog
   							If StringInStr($builtinfunctions,$prog) = 0 Then
   								GUICtrlSetData($cbProgpath,$list,$prog)
   							EndIf
   							MsgBox(64,"Hotkeys Info","Your change is saved")
   						Else
   							MsgBox(16,"Hotkeys Error","Cannot modify to " & $inifile)
   						EndIf
   					Else
   						MsgBox(16,"Hotkeys Error",$err)
   					EndIf
   				Else
   					MsgBox(16,"Hotkeys Error",$err)
   				EndIf
   			EndIf
   		Case $btnRem
   			If GUICtrlRead($rBuiltin) = $GUI_CHECKED Then
   				$prog = GUICtrlRead($cbFunction)
   			Else
   				$prog = GUICtrlRead($cbProgpath)
   			EndIf
   			$keys = IniRead($inifile,"keys",$prog,"")
   			If IniDelete($inifile,"keys",$prog) Then
   				HotKeySet($keys)
   				If GUICtrlRead($rProg) = $GUI_CHECKED Then
   					$list = StringReplace($list,"|" & $prog,"")
   					GUICtrlSetData($cbProgpath,$list)
   				EndIf
   				MsgBox(64,"Hotkeys Info","Your change is saved")
   			Else
   				MsgBox(16,"Hotkeys Error","Cannot modify to " & $inifile)
   			EndIf
   	EndSwitch
   WEnd
EndFunc

Func AlwaysOnTop()
   $activewintitle = WinGetTitle("[ACTIVE]")
   If $activewintitle = "" Then Return
   $topflag = "**alwaysontop**"
   if StringInStr($activewintitle,$topflag) = 0 Then
   	WinSetOnTop($activewintitle,"",1)
   	$newtitle = $activewintitle & $topflag
   Else
   	WinSetOnTop($activewintitle,"",0)
   	$newtitle = StringReplace($activewintitle,$topflag,"")
   EndIf
   WinSetTitle($activewintitle,"",$newtitle)
  fnUnstuck()
EndFunc

Func Min2Tray()
   $activewintitle = WinGetTitle("[ACTIVE]")
   If $activewintitle = "" Then Return
   WinSetState($activewintitle,"",@SW_HIDE)
   $trayid = TrayCreateItem($activewintitle,$hiddenwinmenu)
   IniWrite($inifile,"hiddenwin",$activewintitle,$trayid)
   $hiddenwin = IniReadSection($inifile,"hiddenwin")
   TrayItemSetText($hiddenwinmenu,"Hidden Windows(" & $hiddenwin[0][0] & ")")
  fnUnstuck()
EndFunc

Func fnShowWin($trayid)
   $activewintitle = TrayItemGetText($trayid)
   WinSetState($activewintitle,"",@SW_SHOW)
   TrayItemDelete($trayid)
   IniDelete($inifile,"hiddenwin",$activewintitle)
   $hiddenwin = IniReadSection($inifile,"hiddenwin")
   If @error = 1 Then
   	$n = 0
   Else
   	$n = $hiddenwin[0][0]
   EndIf
   TrayItemSetText($hiddenwinmenu,"Hidden Windows(" & $n & ")")
EndFunc

Func fnShowAll()
   $hiddenwin = IniReadSection($inifile,"hiddenwin")
   If @error = 1 Then Return
   For $i = 1 To $hiddenwin[0][0]
   	WinSetState($hiddenwin[$i][0],"",@SW_SHOW)
   	TrayItemDelete($hiddenwin[$i][1])
   Next
   IniDelete($inifile,"hiddenwin")
   TrayItemSetText($hiddenwinmenu,"Hidden Windows(0)")
EndFunc

Func MoveBottomEdge()
   $workarea = Desktop_Workarea()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",Default,$workarea[3] + $workarea[1] - $winpos[3])
  fnUnstuck()
EndFunc

Func MoveLeftEdge()
   $workarea = Desktop_Workarea()
   WinMove("[ACTIVE]","",$workarea[0],Default)
  fnUnstuck()
EndFunc

Func MoveTopEdge()
   $workarea = Desktop_Workarea()
   WinMove("[ACTIVE]","",Default,$workarea[1])
  fnUnstuck()
EndFunc

Func MoveRightEdge()
   $workarea = Desktop_Workarea()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",$workarea[2] + $workarea[0] - $winpos[2],Default)
  fnUnstuck()
EndFunc

Func MoveLeftHalf()
   $workarea = Desktop_Workarea()
   WinMove("[ACTIVE]","",$workarea[0],$workarea[1],$workarea[2]/2,$workarea[3])
  fnUnstuck()
EndFunc

Func MoveRightHalf()
   $workarea = Desktop_Workarea()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",$workarea[2] + $workarea[0] - $winpos[2],$workarea[1],$workarea[2]/2,$workarea[3])
  fnUnstuck()
EndFunc

Func MoveTopHalf()
   $workarea = Desktop_Workarea()
   WinMove("[ACTIVE]","",$workarea[0],$workarea[1],$workarea[2],$workarea[3]/2)
  fnUnstuck()
EndFunc

Func MoveBottomHalf()
   $workarea = Desktop_Workarea()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",$workarea[0],$workarea[3] + $workarea[1] - $winpos[3],$workarea[2],$workarea[3]/2)
  fnUnstuck()
EndFunc

Func MoveUp()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",Default,$winpos[1] - $movestep)
  fnUnstuck()
EndFunc

Func MoveDown()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",Default,$winpos[1] + $movestep)
  fnUnstuck()
EndFunc

Func MoveLeft()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",$winpos[0] - $movestep,Default)
  fnUnstuck()
EndFunc

Func MoveRight()
   $winpos = WinGetPos("[ACTIVE]")
   WinMove("[ACTIVE]","",$winpos[0] + $movestep,Default)
  fnUnstuck()
EndFunc

Func Desktop_Workarea()
   dim $workarea[4] ;x,y,w,h
   $tbpos = Find_Taskbar()
   Select
   	Case StringInStr($tbpos[4],"Hidden")
   		$workarea[0] = 0
   		$workarea[1] = 0
   		$workarea[2] = @DesktopWidth
   		$workarea[3] = @DesktopHeight
   	Case StringInStr($tbpos[4],"Bottom")
   		$workarea[0] = 0
   		$workarea[1] = 0
   		$workarea[2] = @DesktopWidth
   		$workarea[3] = @DesktopHeight - $tbpos[3]
   	Case StringInStr($tbpos[4],"Top")
   		$workarea[0] = 0
   		$workarea[1] = $tbpos[3]
   		$workarea[2] = @DesktopWidth
   		$workarea[3] = @DesktopHeight - $tbpos[3]
   	Case StringInStr($tbpos[4],"Left")
   		$workarea[0] = $tbpos[2]
   		$workarea[1] = 0
   		$workarea[2] = @DesktopWidth - $tbpos[2]
   		$workarea[3] = @DesktopHeight
   	Case StringInStr($tbpos[4],"Right")
   		$workarea[0] = 0
   		$workarea[1] = 0
   		$workarea[2] = @DesktopWidth - $tbpos[2]
   		$workarea[3] = @DesktopHeight
   EndSelect
   Return $workarea
EndFunc

Func Find_Taskbar()
    Dim $aTaskBarPos[5] ;x,y,w,h,location
    ; Find taskbar
    Local $iPrevMode = AutoItSetOption("WinTitleMatchMode", 4)
    Local $aTaskBar_Pos = WinGetPos("classname=Shell_TrayWnd")
   ;Local $aTaskBar_Pos = ControlGetPos("classname=Shell_TrayWnd", "", "ToolbarWindow322")
    AutoItSetOption("WinTitleMatchMode", $iPrevMode)

    ; If error in finding taskbar
    If Not IsArray($aTaskBar_Pos) Then Return SetError(1, 0, "")
    ; Determine position of taskbar
    Select
        Case $aTaskBar_Pos[1] > 0
            ; TaskBar at BOTTOM
            If $aTaskBar_Pos[1] > @DesktopHeight - $aTaskBar_Pos[3] + 2 Then
                $aTaskBarPos[4] = "Bottom Hidden"
            Else
                $aTaskBarPos[4] = "Bottom"
            EndIf
        Case $aTaskBar_Pos[0] > 0
            ; TaskBar at RIGHT
            If $aTaskBar_Pos[0] > @DesktopWidth - $aTaskBar_Pos[2] + 2 Then
                $aTaskBarPos[4] = "Right Hidden"
            Else
                $aTaskBarPos[4] = "Right"
            EndIf
        Case $aTaskBar_Pos[2] >= @DesktopWidth
            ; TaskBar at TOP
            If $aTaskBar_Pos[1] < -2 Then
                $aTaskBarPos[4] = "Top Hidden"
            Else
                $aTaskBarPos[4] = "Top"
            EndIf
        Case $aTaskBar_Pos[3] >= @DesktopHeight
            ; TaskBar at LEFT
            If $aTaskBar_Pos[0] < -2 Then
                $aTaskBarPos[4] = "Left Hidden"
            Else
                $aTaskBarPos[4] = "Left"
            EndIf
    EndSelect
    For $i = 0 To 3
        $aTaskBarPos[$i] = $aTaskBar_Pos[$i]
    Next
    Return $aTaskBarPos
EndFunc

Func WindowTransparency()
   $activewintitle = WinGetTitle("[ACTIVE]")
   If $activewintitle = "" Then Return
   #Region ### START Koda GUI section ### Form=2
   $fTrans = GUICreate("Set Transparency", 256, 81, 192, 114)
   $Slider1 = GUICtrlCreateSlider(0, 40, 255, 37)
   GUICtrlSetLimit(-1, 255, 0)
   GUICtrlSetData(-1, 255)
   $Label1 = GUICtrlCreateLabel("0", 8, 24, 10, 17)
   $Label2 = GUICtrlCreateLabel("255", 229, 24, 22, 17)
   $Label3 = GUICtrlCreateLabel("invisible", 8, 8, 41, 17)
   $Label4 = GUICtrlCreateLabel("solid", 226, 8, 25, 17)
   $Input1 = GUICtrlCreateInput("255", 117, 8, 25, 21)
   GUICtrlSetLimit(-1, 3)
   GUICtrlSetState(-1, $GUI_DISABLE)
   GUISetState(@SW_SHOW)
   #EndRegion ### END Koda GUI section ###
   While 1
   	$msg2 = GUIGetMsg()
   	Select
   		Case $msg2 = 0
   			ContinueLoop
   		Case $msg2 = $GUI_EVENT_CLOSE
   			GUIDelete($fTrans)
   			ExitLoop
   		Case $msg2 = $Slider1
   			$Slider1val = GUICtrlRead($Slider1)
   			GUICtrlSetData($Input1,$Slider1val)
   			WinSetTrans($activewintitle,"",$Slider1val)
   	EndSelect
   WEnd
  fnUnstuck()
EndFunc

Func TaskbarHideButton()
  $activewintitle = WinGetTitle("[ACTIVE]")
   If $activewintitle = "" Then Return
   $hToolbar = _GUICtrlToolbar_FindToolbar("[CLASS:Shell_TrayWnd]", $activewintitle)
    If $hToolbar = 0 Then Return False
    For $i = 0 To _GUICtrlToolbar_ButtonCount($hToolbar)-1
        $cId = _GUICtrlToolbar_IndexToCommand($hToolbar, $i)
        If _GUICtrlToolbar_GetButtonText($hToolbar, $cId) = $activewintitle Then
   		Return _GUICtrlToolbar_HideButton($hToolbar, $cId, True)
        EndIf
    Next
  fnUnstuck()
  Return False
EndFunc

Func TaskbarShowButton()
  $activewintitle = WinGetTitle("[ACTIVE]")
   If $activewintitle = "" Then Return
   $hToolbar = _GUICtrlToolbar_FindToolbar("[CLASS:Shell_TrayWnd]", $activewintitle)
    If $hToolbar = 0 Then Return False
    For $i = 0 To _GUICtrlToolbar_ButtonCount($hToolbar)-1
        $cId = _GUICtrlToolbar_IndexToCommand($hToolbar, $i)
        If _GUICtrlToolbar_GetButtonText($hToolbar, $cId) = $activewintitle Then
            Return _GUICtrlToolbar_HideButton($hToolbar, $cId, False)
        EndIf
    Next
  fnUnstuck()
  Return False
EndFunc